<?php

namespace Jxgl\Controller;
use Think\Page;
use Think\Controller;


/**
 * 代课管理控制器
 * @author水月居 <singliang@163.com>
 */
class DaikeController extends Controller {

    /**
     * 教学管理-代课登记首页
    */
    public function index(){ 

    	//获取班级列表（从校本课程中取得）
        $classlist=D('XbClass')
        ->field('class,headteacher')
        ->order(array('sort'=>'asc')) ->select();
        //获取最近的3次代课记录  	
	    $daike=D('JxDaike');
	    $order=array('teacher'=>'desc','d_date'=>'asc','id'=>'asc');
	    $tlist=$daike->order($order)->limit('0,3')->select();
	     if(IS_POST){
	     	$data=array(
              'teacher'=>I('post.teacher'),
              'class'  =>I('post.class'),
              'category'=>I('post.category'),
              'd_year'    =>date('Y'),
              'd_month'  => I('month'),   
              'd_date'   =>I('month').I('day'),
              'd_jieci' =>I('week').I('d_jieci'),
              'shiyou'  =>I('post.shiyou'),
              'number'  =>I('post.number'),
	     		);
	     	$data=$daike->create($data);      
          //保存表单数据                 
            $result = $daike->add();
             if(!$result ){               
                  $this->error($daike->getError());
                }else{
               // echo '数据更新失败！';
                $this->success('代课登记成功', Cookie('__forward__'));               
                }              
           }
	    $this->assign('classlist',$classlist);
		  $this->assign('list', $tlist);
      $this->display();
    }

    //编辑代课记录

   public function edit(){
    $id=I('get.id');
   	$daike=D('JxDaike');
   	$daike_content=D('JxDaike')->where(array('status' => 1, 'id' => $id))->find();
	 if (!$daike_content) {
       $this->error('代课记录不存在！');
        }
      if(!($daike_content['uid'] == is_login() ||is_administrator(is_login()))){
       	$this->error('非代课登记人不能修改操作！');
       }
    $this->cur=D('JxDaike')->find($id);
    //dump($this->cur);
           if(IS_POST){
          //dump($_POST) ;          
           //保存表单数据                 
            $result = $daike->save(I("post."));
             if(!$result ){               
                  $this->error($daike->getError());
                }else{
               // echo '数据更新失败！';
                $this->success('更新成功', Cookie('__forward__'));               
                }              
        }
     $this->display();
   }

       //作业查询

   public function search(){
  	 //获取最近的3次代课记录
  	    $month=I('d_month');
   	    if(!$month) {
   	    	$map['d_month']=date('n').'月';
   	    } else{
   	    	$map['d_month']=$month;
   	    }  	
	    $daike=D('JxDaike');
	    $order=array('teacher'=>'desc','d_date'=>'asc','id'=>'asc');
	    $tlist=$daike->order($order)->where($map)->select();
	    $this->assign('list', $tlist);
	    $this->display();
   }

   // 删除代课记录
   public function delete($id=0){
        $id=I('get.id');
	   	$daike_content=D('JxDaike')->where(array('status' => 1, 'id' => $id))->find();
	   	if (!$daike_content) {
            $this->error('代课记录不存在！');
        }
	   	//验证是否有管理权限$daike_content['uid'] == is_login() || is_administrator(is_login())
	   if($daike_content['uid'] == is_login() ||is_administrator(is_login())){
           //$res = D('daike')->where(array('status' => 1, 'id' => $id))->setField('status', 0);
	   	   $result=D('JxDaike')->delete($id);
		   if(!$result ){               
		      $this->error($daike->getError());
		    }else{	  
		    $this->success('删除成功', Cookie('__forward__'));               
		    }    
	   	// echo "删除代课记录".I('id');

	   }else{
	  $this->error('非代课登记人操作！');
	   }

   }
   public function show(){
   
         //获取班级列表（从校本课程中取得）
        $classlist=D('XbClass')->field('class,headteacher')->order(array('sort'=>'asc')) ->select();
        $month=I('d_month');
        $class=I('class');
   	    if(empty($class)) {
   	        unset($map['class']);
   	        } else{
   	    	$map['class']=$class;
   	     }	
   	    if(!$month) {
   	    	$map['d_month']=date('n').'月';
   	        } else{
   	    	$map['d_month']=$month;
   	     }	
   	    //dump($map);
	    $daike=D('JxDaike');
	    $order=array('d_date'=>'desc');
	    $tlist=$daike->order($order)->where($map)->select();
	   //echo $daike->getLastSql();
	    //dump($tlist);
	    $this->assign('list', $tlist);
	    $this->assign('classlist',$classlist);
	    $this->display();
   }
} 